import {ComponentAnchor, ComponentOption} from "../../ComponentOption.ts";
import {distance2d} from "../../util.ts";
import EventConstant from "../../EventConstant.ts";
import {LinkOption} from "../../LinkOption.ts";
import EventBus from "../../EventBus.ts";
import {onMounted, onUnmounted} from "vue";

export function useHandleLinkEdit(panelStatus: any, panelApi: any, co: ComponentOption, panelEvents: any, eventBus: EventBus) {
    function handleMousemove(event: MouseEvent) {
        if (panelStatus.drawingLink.value) {//如果正在创建连接线，这里计算出连接到哪个锚点上
            event.preventDefault();
            event.stopPropagation();
            const panelPosition = panelApi.getPanelPosition()
            const offsetPosition = {x: event.clientX - panelPosition.left, y: event.clientY - panelPosition.top}
            //找到离鼠标最近的那个锚点
            let nearestAnchor: ComponentAnchor | null = null;
            let minDistance = Infinity;
            for (let anchor of co.anchors) {
                const distance = distance2d(anchor.point.x, anchor.point.y, offsetPosition.x, offsetPosition.y)
                if (distance < minDistance) {
                    nearestAnchor = anchor
                    minDistance = distance
                }
            }
            if (nearestAnchor) {
                //找到最近的anchor之后，吸附过去
                const drawingLinkOption = panelStatus.drawingLinkOption.value;
                const pointsCount = drawingLinkOption.points.length;
                //连接线的最后一个点吸附到最近的锚点
                drawingLinkOption.points[pointsCount - 1] = nearestAnchor.point.copy()
                drawingLinkOption.endAnchor = nearestAnchor
            }
        }
        //panel正在拖动连接线的关键点，如果拖动的是开始或结束，就找到最近的锚点
        if (panelStatus.draggingLinePoint.value) {
            if (panelStatus.draggingLinePointIsStart.value || panelStatus.draggingLinePointIsEnd.value) {
                event.preventDefault();
                event.stopPropagation();
                const panelPosition = panelApi.getPanelPosition()
                const offsetPosition = {x: event.clientX - panelPosition.left, y: event.clientY - panelPosition.top}
                //找到离鼠标最近的那个锚点
                let nearestAnchor: ComponentAnchor | null = null;
                let minDistance = Infinity;
                for (let anchor of co.anchors) {
                    const distance = distance2d(anchor.point.x, anchor.point.y, offsetPosition.x, offsetPosition.y)
                    if (distance < minDistance) {
                        nearestAnchor = anchor
                        minDistance = distance
                    }
                }
                if (nearestAnchor) {
                    //找到最近的anchor之后，吸附过去
                    const draggingLinkOption = panelStatus.draggingLinePointOption.value;
                    const pointsCount = draggingLinkOption.points.length;
                    //连接线的最后一个点吸附到最近的锚点
                    if (panelStatus.draggingLinePointIsEnd.value) {
                        const lastPoint = draggingLinkOption.points[pointsCount - 1]
                        lastPoint.x = nearestAnchor.point.x
                        lastPoint.y = nearestAnchor.point.y
                        draggingLinkOption.endAnchor = nearestAnchor
                    }
                    if (panelStatus.draggingLinePointIsStart.value) {
                        const firstPoint = draggingLinkOption.points[0]
                        firstPoint.x = nearestAnchor.point.x
                        firstPoint.y = nearestAnchor.point.y
                        draggingLinkOption.startAnchor = nearestAnchor
                    }
                }
            }
        }
    }

    //停止监听linkFindingEnd
    let exitLinkLookingforEnd: Function | null = null;
    onMounted(() => {
        //监听连接线完成创建事件
        eventBus.subscribe(EventConstant.PANEL_LINK_ENDED, () => {
            if (exitLinkLookingforEnd) {
                exitLinkLookingforEnd()
            }
        })
    })
    onUnmounted(() => {
        if (exitLinkLookingforEnd) {
            exitLinkLookingforEnd()
        }
    })

    function handleStartLink(event: MouseEvent) {
        event.stopPropagation()
        event.preventDefault()
        co.selected = false;
        panelEvents.drawLinkStart(co, co.anchors[0], event)
        //开始监听连接线绘制时找终点的事件
        exitLinkLookingforEnd = eventBus.subscribe(EventConstant.PANEL_LINK_LOOKING_FOR_END, (event: any, lo: LinkOption) => {
            //距离最近的开始锚点
            //找到离鼠标最近的那个锚点
            let nearestAnchor: ComponentAnchor | null = null;
            let minDistance = Infinity;
            for (let anchor of co.anchors) {
                const distance = distance2d(anchor.point.x, anchor.point.y, event.x, event.y)
                if (distance < minDistance) {
                    nearestAnchor = anchor
                    minDistance = distance
                }
            }

            if (nearestAnchor) {
                //找到最近的anchor之后，吸附过去
                lo.points[0] = nearestAnchor.point.copy()
                lo.startAnchor = nearestAnchor
            }
        })
    }

    return {handleMousemove, handleStartLink}
}
